package org.qengine.controller;

import org.qengine.entity.PaperInfoEntity;
import org.qengine.entity.QuestionEntity;
import org.qengine.entity.StuInfoEntity;
import org.qengine.entity.UserAnswersEntity;
import org.qengine.service.PaperInfoService;
import org.qengine.service.QuestionService;
import org.qengine.service.StuInfoService;
import org.qengine.service.UserAnswersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/paperInfo")
public class PaperInfoController {

    @Autowired
    private PaperInfoService paperInfoService;

    @Autowired
    private StuInfoService stuInfoService;

    @Autowired
    private QuestionService questionService;

    @Autowired
    private UserAnswersService userAnswersService;

    // 保存试卷
    @GetMapping("/savePapers")
    public ResponseEntity<List<QuestionEntity>> getPaperQuestions(@PathVariable Long paperID) {
        PaperInfoEntity paperInfo = paperInfoService.getPaperInfoById(paperID);
        if (paperInfo == null) {
            return ResponseEntity.notFound().build();
        }

        String questionListStr = paperInfo.getQuestionList();
        List<QuestionEntity> questionEntities = new ArrayList<>();
        if (questionListStr != null) {
            String[] questionIds = questionListStr.split(",");
            for (String questionId : questionIds) {
                QuestionEntity question = questionService.getQuestionById(Integer.parseInt(questionId));
                if (question != null) {
                    questionEntities.add(question);
                }
            }
        }
        return ResponseEntity.ok(questionEntities);
    }


    // 获得学生试卷id列表
    @GetMapping("/stuPapers")
    public List<Long> getPaperIDsByStuID(@RequestParam("stuID") Long stuID) {
        List<Long> paperIDs =  paperInfoService.getPaperIDsByStuID(stuID);

        // 更新 stuInfo 的 paperIDS 字段
        stuInfoService.updatePaperIDs(stuID, paperIDs);

        return paperIDs;
    }

    @GetMapping("/{paperID}")
    public PaperInfoEntity getPaperInfoById(@PathVariable Long paperID) {
        return paperInfoService.getPaperInfoById(paperID);
    }

//

    @PostMapping("/updatePaperInfo")
    public String insertStuInfo(@RequestBody PaperInfoEntity paperInfoEntity) {
        int result =  paperInfoService.updatePaperInfo(paperInfoEntity);
        if(result >= 1) {
            return "更新成功";
        } else {
            return "更新失败";
        }
    }

    @RequestMapping("/deletePaperInfo")
    public String deletePaperInfo(@RequestParam("paperID") Long paperID) {
        int result =  paperInfoService.deletePaperInfo(paperID);
        if(result >= 1) {
            return "删除成功";
        } else {
            return "删除失败";
        }
    }


    @RequestMapping("/getAllPaperInfo")
    public List<PaperInfoEntity> getAllPaperInfo() {
        return paperInfoService.getPaperInfo();
    }




    // 根据学生ID和试卷ID获取学生的答卷信息
    @GetMapping("/stuAnswers/{stuID}/{paperID}")
    public UserAnswersEntity getStuAnswersByStuIDAndPaperID(@PathVariable Long stuID, @PathVariable Long paperID) {
        return userAnswersService.getStuAnswersByStuIDAndPaperID(stuID, paperID);
    }

}
